package edu.hhu.controller;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import edu.hhu.basic.utils.FormatUtils;

public class PaiCaiController extends Controller{
	public void index(){
		render("/paicai/query.jsp");
	}
	public void query() {
		String table = getPara("tablename");
		String sql = "select * from data_dictionary where tablename=?";
		List<Record> td = Db.find(sql,table);
		renderJson(td);
		// renderJson(list);
	}
	public void query1() {
		String para = getPara("");
		String sql="SELECT gasfieldcode gas,count(*) wellcount,SUM(c) lcql,SUM(q) lcyl from ((SELECT MAX(SCRQ),wellcode,MAX(LJCQ) c,MAX(LJCY) q  FROM pcdtsjtrue  GROUP BY wellcode) e LEFT OUTER JOIN cjkj d on e.wellcode=d.wellcode)  GROUP BY gasfieldcode";
		System.out.println(sql);
		List<Record> list = Db.find(sql);
		FormatUtils.DealNull(list);
		renderJson(list);
		// renderJson(list);
	}

	public void column2() {
		String tablename = getPara("tablename");
		String sql2 = "SELECT * from data_dictionary where tablename=?";
		List<Record> list = Db.find(sql2, tablename);
		FormatUtils.DealNull(list);
		renderJson(list);
	}

	public void query2() {
		String para = getPara("tiaojian");
		String sql="SELECT wellgroupcode,count(*) wellcount,SUM(c) lcql,SUM(q) lcyl from ((SELECT MAX(SCRQ),wellcode,MAX(LJCQ) c,MAX(LJCY) q  FROM pcdtsjtrue GROUP BY wellcode) e LEFT OUTER JOIN cjkj d on e.wellcode=d.wellcode) where gasfieldcode=? GROUP BY wellgroupcode";
		System.out.println(sql);
		List<Record> list = Db.find(sql,para);
		FormatUtils.DealNull(list);
		if (list == null) {
			list = new ArrayList<Record>();
		}
		renderJson(list);
	}
	public void query3() {
		String para = getPara("tiaojian");
		int pageno = Integer.parseInt(getPara("page"));
		String tablename=getPara("tablename");
		String sql=null;
		String select=null;
		if("jz".equals(tablename)){
			 sql="from (SELECT wellname,wellcode,MAX(SCRQ) scrq,QQ,QL,MAX(LJCQ) ljcq ,MAX(LJCY) ljcy ,PWF,TY   FROM pcdtsjtrue  GROUP BY wellcode) e LEFT OUTER JOIN cjkj a on e.wellcode=a.wellcode where a.wellgroupcode=?";
			System.out.println(sql);
		     select="SELECT a.wellname well,scrq zjscrq,QQ rcq,QL rcy,ljcq lcql ,ljcy lcyl,PWF,TY,a.wellgroupcode";
		}else if("jing".equals(tablename)){
			 sql=" FROM pcdtsjtrue  WHERE wellname=?";
			System.out.println(sql);
		     select="SELECT wellname,SCRQ,QQ,QL,LJCQ,LJCY";

		}else if("pcdtsjtrue".equals(tablename)){
			 sql=" FROM pcdtsjtrue WHERE wellname=?";
			 System.out.println(sql);
			 select="SELECT ID,wellcode,SCRQ,wellname,SCSJ,BJBS,CCCC,TL,TY,DYM,YJYM,PWF,DPWF,MFJK,BX,QQ,QL,CL,YLCQ,LLCQ,LJCQ,YLCY,LLCY,LJCY,PH,RQYQ,BAK";
		}
		Page<Record> page = Db.paginate(pageno, 15, select, sql,para);
		FormatUtils.DealNull(page.getList());
		renderJson(page);
	}
	public void query4() {
		String para = getPara("tiaojian");
		int pageno=Integer.parseInt(getPara("page"));
		String sql="FROM pcdtsjtrue  WHERE wellname=?";
		System.out.println(sql);
		Page<Record> page = Db.paginate(pageno, 20, "SELECT wellname,SCRQ,QQ,QL,LJCQ,LJCY", sql,para);
		FormatUtils.DealNull(page.getList());
		renderJson(page);
	}
	
	public void ziduan() {
		String para = getPara("wellname");
		String sql=" from pcdtsjtrue where wellname=?";
		System.out.println(sql);
		List<Record> list = Db.find(sql,para);
		FormatUtils.DealNull(list);
		if (list == null) {
			list = new ArrayList<Record>();
		}
		renderJson(list);
	}
}
